package com.source.hotelbath.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Process;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/23.
 */

public abstract class BaseApplication extends Application {
    protected static BaseApplication mInstance;
    protected List<WeakReference<Activity>> mWeakActivities = new ArrayList();

    public BaseApplication() {
    }

    protected static BaseApplication getInstance() {
        return mInstance;
    }

    public void onCreate() {
        super.onCreate();
        if(!this.isNoProcessName()) {
            mInstance = this;
            this.setTextSize();
            this.afterCreate();
        }
    }

    private void setTextSize() {
        Resources var1 = this.getResources();
        Configuration var2 = var1.getConfiguration();
        var2.fontScale = 1.0F;
        var1.updateConfiguration(var2, var1.getDisplayMetrics());
    }

    public void exit() {
        this.beforeExit();
        this.finishAllActivity();
        Process.killProcess(Process.myPid());
        System.exit(0);
    }

    public void registerActivity(Activity var1) {
        this.mWeakActivities.add(new WeakReference(var1));
    }

    public void unRegisterActivity(Activity var1) {
        for(int var2 = 0; var2 < this.mWeakActivities.size(); ++var2) {
            if(null != this.mWeakActivities.get(var2) && ((WeakReference)this.mWeakActivities.get(var2)).get() == var1) {
                this.mWeakActivities.remove(var2);
            }
        }

    }

    public void finishAllActivity() {
        if(null != this.mWeakActivities) {
            for(int var1 = 0; var1 < this.mWeakActivities.size(); ++var1) {
                WeakReference var2 = (WeakReference)this.mWeakActivities.get(var1);
                Activity var3 = (Activity)var2.get();
                this.mWeakActivities.remove(var1);
                --var1;
                if(null != var3 && !var3.isFinishing()) {
                    var3.finish();
                }
            }

            this.mWeakActivities.clear();
        }
    }

    public void finishActByActName(String var1) {
        if(null != this.mWeakActivities) {
            for(int var2 = 0; var2 < this.mWeakActivities.size(); ++var2) {
                WeakReference var3 = (WeakReference)this.mWeakActivities.get(var2);
                Activity var4 = (Activity)var3.get();
                if(null != var4 && !var4.isFinishing() && var4.getClass().getName().equals(var1)) {
                    this.mWeakActivities.remove(var2);
                    --var2;
                    var4.finish();
                }
            }

        }
    }

    private boolean isNoProcessName() {
        boolean var1 = false;
        int var2 = Process.myPid();
        String var3 = null;
        ActivityManager var4 = (ActivityManager)this.getSystemService("activity");
        List var5 = var4.getRunningAppProcesses();
        Iterator var6 = var5.iterator();
        PackageManager var7 = this.getPackageManager();

        while(var6.hasNext()) {
            ActivityManager.RunningAppProcessInfo var8 = (ActivityManager.RunningAppProcessInfo)((ActivityManager.RunningAppProcessInfo)var6.next());

            try {
                if(var8.pid == var2) {
                    var7.getApplicationLabel(var7.getApplicationInfo(var8.processName, 128));
                    var3 = var8.processName;
                }
            } catch (Exception var10) {
                ;
            }
        }

        if(var3 == null || var3.equals("")) {
            var1 = true;
        }

        return var1;
    }

    public boolean isTopActivity(Activity var1) {
        boolean var2 = false;
        ActivityManager var3 = (ActivityManager)var1.getSystemService("activity");
        List var4 = var3.getRunningTasks(1);
        if(!var4.isEmpty() && ((ActivityManager.RunningTaskInfo)var4.get(0)).topActivity.getPackageName().equals(var1.getPackageName()) && ((ActivityManager.RunningTaskInfo)var4.get(0)).topActivity.getClassName().equals(var1.getClass().getName())) {
            var2 = true;
        }

        return var2;
    }

    public boolean isApplicationBroughtToBackground() {
        ActivityManager var1 = (ActivityManager)this.getSystemService("activity");
        List var2 = var1.getRunningTasks(1);
        if(!var2.isEmpty()) {
            ComponentName var3 = ((ActivityManager.RunningTaskInfo)var2.get(0)).topActivity;
            if(!var3.getPackageName().equals(this.getPackageName())) {
                return true;
            }
        }

        return false;
    }

    public boolean isDegug() {
        boolean var1 = 0 != (this.getApplicationInfo().flags & 2);
        return var1;
    }

    protected abstract void afterCreate();

    protected abstract void beforeExit();
}
